﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Xml.Linq;
using EnvDTE;
using Extentions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using NPOI.HPSF;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using Org.BouncyCastle.Math.EC;
using Senparc.Weixin.MP.Entities.Request;
using TrainingReminderSystem.Bll;
using TrainingReminderSystem.Dal;
using TrainingReminderSystem.Model;
//using TrainingReminderSystemWxOpen.Bll.MessageHandlers;

namespace Test
{
    class Program
    {
        static async Task Main()
        {
            TestNewDb();
            //App.Main2();
            //await TestBll.TestGetListAsyn();
            //TestBll.TestGetList();
            //TestBll.TestWxOpenSdkHelperGetInfoBySonPrimary();
            //TestBll.TestManyDb();
            //TestBll.TestTaskDelay();
            return;
            //
            //await TestBll.FunctionsLike();
            //TestBll.TestCacheForeach();

            //            string xml = @"<xml>
            //    <AppId><![CDATA[wx6e7bed47d2098465]]></AppId>
            //    <Encrypt><![CDATA[/RWeOUN4J839ynYH960Duj2k0WwvSfwq2dBFQ1NG78v9CZKbiCk5F8Fq/RGV0oFALoy+2L7KDe+1EbitZK7T4KtHMxntcEeQDxhWGdbeyTYJStcl7pudKA/ltfPP2nDbMrvmdl6JAX/XY4El0XEaYMrxegU3B1aUMZJ1GDjz9pIFv5+DPWj2mf7mhwIKcJSfeMctifS5UyJHKmIM+gnPQjtcKsVdnjaLckEScUsKKTygeG9IOtWFfE720W6g5UfOg3yxPFCUrqjWKRTue7g0yT0vQyb0L5cODKh1bCjIRcgikJsRVo3tAX1QO/CrHzKEEDqaD3Rx1hRkvRl/2KQlFd/DkhOzWp1LmpsCZQiNj0Fdc41aVleaFrwUQ8svg9Wt2iJLDhdqlz1Us2Pb6Ayx3dJLhXtb4ynJGsPFq0N8RrCuLgU391BpAiga5JBcjzS50pfvBBEEPQDyAE7znPphrA==]]></Encrypt>
            //</xml>";
//            string xml = @"
//<xml>
//  <ToUserName><![CDATA[gh_08152fc67d76]]></ToUserName>
//  <FromUserName><![CDATA[oDU6x5MJEakdydtWo7La_ZRx-wts]]></FromUserName>
//  <CreateTime>1661269034</CreateTime>
//  <MsgType><![CDATA[event]]></MsgType>
//  <Event><![CDATA[weapp_audit_success]]></Event>
//  <SuccTime>1661269034</SuccTime>
//</xml>
//";
//            var postModel = new PostModel()
//            {
//                AppId = "wx6e7bed47d2098465",
//                Msg_Signature = "066523677e42a15c64a725ba0058cd7651174624",
//                Signature = "9c092b226e43fc458d743e85eafae351209a41e5",
//                Timestamp = "1661267043",
//                Nonce = "1441155409",

//                Token = "wxopentoken",
//                EncodingAESKey = "abcdefghijklmnopqrstuvwxyz1234567890abcdefg"
//            };
//            var messageHandler = new CustomMessageHandler(XDocument.Parse(xml), postModel);
//            await messageHandler.ExecuteAsync(new System.Threading.CancellationToken());//执行微信处理过程（关键）
//            Console.Read();
        }

        static void TestNewDb()
        {
            var dbTemp = new DbEntities();
            Console.WriteLine("声明初始化一个db");
            const int maxLength = 10000;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (var i = 0; i < maxLength; i++)
            {
                var bll = new TestBll();
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
            sw.Reset();
            sw.Start();
            for (var i = 0; i < maxLength; i++)
            {
                var db = new DbEntities();
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
            Console.ReadLine();
        }

        #region 接口事件对比

        /// <summary>
        /// app类
        /// </summary>
        /// <typeparam name="IShowName"></typeparam>
        public class App
        {
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; set; } = "初始Name";

            /// <summary>
            /// 执行
            /// </summary>
            /// <returns></returns>
            public void Do()
            {
                if (this._showName != null)
                {
                    this.Name = this._showName.ChangeName(Name);
                }
                Console.WriteLine(Name);
            }

            public delegate string ChangeNameDelegate(string name);

            public event ChangeNameDelegate ChangeNameEvent;

            /// <summary>
            /// 执行2
            /// </summary>
            /// <returns></returns>
            public void Do2()
            {
                string a = this.ChangeNameEvent != null ? this.ChangeNameEvent(Name) : Name;
                this.Name = a;
                Console.WriteLine(Name);
            }

            private IShowName _showName { get; set; }

            public void Regiter(IShowName showName)
            {
                this._showName = showName;
            }

            private ChangeNameDelegate delegete = null;

            public void Regiter2(ChangeNameDelegate changeNameDelegate)
            {
                this.delegete = changeNameDelegate;

                this.ChangeNameEvent += this.delegete;
            }

            /// <summary>
            /// 入口方法
            /// </summary>
            public static void Main2()
            {
                App app = new App();
                IShowName a = new ChangeNameClass();
                app.Regiter(a);
                app.Do();

                app.Regiter2(ChangeNameClass2.ChangeName);
                app.Do2();

                //数据库拼接
                var db = DbEntitiesHelper.GetDbEntities(true);
                //ToBeDealMsg.Project.ProjectID
                //IQueryable<ToBeDealMsg> tempList = db.ToBeDealMsg.ExOrderBy("Project.ProjectID", false);
                //var sql = tempList.ExGetSql();
                //Console.WriteLine(sql);
                //var list = tempList.Take(1).ToList();
                //Console.WriteLine(list.ExToJson());
                //后面这个就是错的,Project里的是List<ToBeDealMsg>
                //IQueryable<TrainingReminderSystem.Model.Project> tempList2 = db.Project.ExOrderBy("ToBeDealMsg.ID", false);
                //var sql2 = tempList.ExGetSql();
                //Console.WriteLine(sql2);
                Console.ReadLine();
            }
        }

        public interface IShowName
        {
            public string ChangeName(string name);

            //public string ChangeName2(string name);

            //public string ChangeName3(string name);
        }

        /// <summary>
        /// 实现类
        /// </summary>
        public class ChangeNameClass : IShowName
        {
            public string ChangeName(string name2)
            {
                name2 += "_执行实现类";
                return name2;
            }
        }

        /// <summary>
        /// 实现类2
        /// </summary>
        public static class ChangeNameClass2
        {
            public static string ChangeName(string name)
            {
                name += "_执行事件实现";
                return name;
            }
        }

        #endregion

        static void Main555(string[] args)
        {
            var emp = new SysEmployee();
            emp.ExCopyFrom(null);
            System.Console.WriteLine(emp == null);
            Console.Read();
            return;
            string fileName = "F:\\码畜生成代码\\库存管理系统\\后台项目\\PcWebApi.Model\\Auto\\SysEmployee.cs";
            OpenFileAtLine(fileName);
            decimal? a = null;
            var b = a * 100;
            Console.WriteLine(b);
            Console.ReadLine();
        }

        private static void OpenFileAtLine(string file, int line = 0)
        {
            DTE dte = (DTE)MarshalHelper.GetActiveObject("VisualStudio.DTE.17.0");
            dte.MainWindow.Visible = true;
            dte.ExecuteCommand("File.OpenFile", file);
            dte.ExecuteCommand("Edit.GoTo", line.ToString());
        }

        static void Main222(string[] args)
        {
            //ProjectBll projectBll = new ProjectBll();
            //var list = projectBll.GetModel(1);
            //Console.WriteLine(list.ExToJson());
            //var r = projectBll.Add(new Project()
            //{
            //    ProjectName = "1232",
            //    UrlDomain = "321321"
            //});
            // Console.WriteLine(r.ExToJson());
        }

        static void Main22222(string[] args)
        {
            //System.Text.RegularExpressions.Regex rg2 = new System.Text.RegularExpressions.Regex("[\\s]*-?[1-9]?\\d*$|^0[\\s\\S]*$");
            //var r1 = rg2.IsMatch(" 12321 ");
            //var r2 = rg2.IsMatch("-0");
            //var r3 = rg2.IsMatch("0");
            //var r4 = rg2.IsMatch(" -999321");
            //var r5 = rg2.IsMatch("-320");
            //Console.WriteLine(r1);
            //Console.WriteLine(r2);
            //Console.WriteLine(r3);
            //Console.WriteLine(r4);
            //Console.WriteLine(r5);
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("=\\s*(-?[1-9]?[0-9]*|0)[\\s,;]*$");
            bool result = rg.IsMatch(" aa =     100 , ");
            bool result2 = rg.IsMatch("中文9 =-1111110 ; ");
            bool result3 = rg.IsMatch("\r\n aa6   =  5 \r\n  ");
            bool result4 = rg.IsMatch("  aa5  =  中1 ,\r\n");
            bool result5 = rg.IsMatch("\r\n aa5中文描述 \r\n");
            Console.WriteLine(result);
            Console.WriteLine(result2);
            Console.WriteLine(result3);
            Console.WriteLine(result4);
            Console.WriteLine(result5);
            Console.ReadLine();
            return;

            var bll = new TestBll();
            bll.TestRedirectUrl();
            //bll.TestRoslyn();
            return;
            //bll.TestBinaryFormatter();
            Console.WriteLine(3 / 2);
            bll.TestLanguage();
            //bll.TestExCopy();
            //bll.TestGetListByPage();
            //bll.TestPinYin();
            //bll.CheckDb();
            Console.ReadLine();
            return;
            IPAddress[] IPs = Dns.GetHostAddresses("wwwwebapi.scmrrj.com");
            var ip = IPs[0].ToString();
            Console.WriteLine(ip);

            //var bll = new TestBll();
            //bll.TestChangeDetail();
            //string a = LanguageHelper.FileName;
            //Console.WriteLine(a);
            //Console.Read();
        }

        //static void Main3333(string[] args)
        //{
        //    Stopwatch sw = new Stopwatch();
        //    sw.Start();
        //    var bll = new ProjectBll();
        //    System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues> temp =
        //        new System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>();
        //    temp.Add("ProjectName", "测试");
        //    IFormCollection form = new FormCollection(temp);
        //    var list = bll.GetListByPage(1, 10);
        //    sw.Stop();
        //    Console.WriteLine(list.Total + " 时间间隔:" + sw.ElapsedMilliseconds);
        //    string a = "重庆,中国庆祝,重量";
        //    var b = ToolHelper.GetPy(a);
        //    Console.WriteLine(a + "    " + b);
        //    var ts = new TimeSpan(0, 0, 1200);
        //    Console.WriteLine(ts);

        //    Console.Read();
        //}
    }
}
